<?php
/**
 * User: micah
 * Date: 11-8-21
 * Time: 下午8:25
 * Desc: UserService 用户登录，查找，验证密码等操作
 */

class UserService
{
    private $hashPasswordKey = "password";

    //输入用户名和密码，如果验证成功，则返回用户，如果返回不争功，则返回错误代码
    public function validate($username, $password)
    {
        $user = $this->getUserByName($username);
        $errorCode = UserConstant::ERROR_NONE;
        if ($user === null)
            $errorCode = UserConstant::ERROR_USERNAME_INVALID;
        else if (!$this->validatePassword($password, $user[UserConstant::USER_TABLE_COLUMN_PASSWORD]))
            $errorCode = UserConstant::ERROR_PASSWORD_INVALID;
        return $errorCode == UserConstant::ERROR_NONE ? $user : $errorCode;

    }

    /**
     * @desc   根据用户的id查找用户信息
     * @param  $id
     * @return 如果该用户存在，则取出该用户，如果不存在，则返回null
     */
    public function getUserById($id)
    {
        return $this->getUserDao()->findUserById($id);
    }

    /**
     * @desc   根据用户的username查找用户信息
     * @param  $username
     * @return 如果该用户存在，则取出该用户，如果不存在，则返回null
     */
    public function getUserByName($username)
    {
        return $this->getUserDao()->findUserByName($username);
    }

    /**
     * @param  $username
     * @param  $password
     * @param string $email
     * @param string $profile
     * @return 如果注册成功，则返回该用户信息，如果注册失败，则返回失败code
     */
    public function register($username, $password, $group = 0, $email = "", $profile = "")
    {
        if (!$username) {
            return UserConstant::ERROR_USERNAME_INVALID;
        }
        if (!$password) {
            return UserConstant::ERROR_PASSWORD_INVALID;
        }
        if(!is_numeric($group)){
            return UserConstant::ERROR_GROUP_INVALID;
        }
        if ($this->getUserByName($username)) {
            return UserConstant::ERROR_USERNAME_EXIST;
        }
        $user = array(
            UserConstant::USER_TABLE_COLUMN_USERNAME => $username,
            UserConstant::USER_TABLE_COLUMN_PASSWORD => $password,
            UserConstant::USER_TABLE_COLUMN_EMAIL => $email,
            UserConstant::USER_TABLE_COLUMN_GROUP => $group,
            UserConstant::USER_TABLE_COLUMN_PROFILE => $profile,
        );
        $addUser = $this->getUserDao()->addUser($user);
        if ($addUser === null) {
            return UserConstant::ERROR_USER_ADD_FAIL;
        }
        return $addUser;
    }

    public function authenticate($username, $password)
    {
        $_identity = new UserIdentity($username, $password);
        if (!$_identity->authenticate())
            $this->addError('password', 'Incorrect username or password.');
    }

    /**
     * Checks if the given password is correct.
     * @param string the password to be validated
     * @param string right password (table password)
     * @return boolean whether the password is valid
     */
    public function validatePassword($password, $rightPassword)
    {
        return $this->hashPassword($password, $this->hashPasswordKey) === $rightPassword;
    }

    /**
     * Generates the password hash.
     * @param string password
     * @param string salt
     * @return string hash
     */
    public function hashPassword($password, $key = "")
    {
        return md5($key . $password);
    }

    public function getUserDao()
    {
        return UserDaoFactory::getInstance()->createUserDao();
    }
}
